const jwt = require('koa-jwt')
const { secret } = require("../config")

const Router = require('koa-router')
const router = new Router({ prefix: '/users' })

const {
    checkUserExist,
    find, findById,
    create, update,
    delete: del,
    login
} = require('../controllers/users')

// 认证，解析token，并且从token中获取用户信息
const auth = jwt({ secret })

router.get('/', find)
router.get('/:id', checkUserExist, findById)
router.post('/', auth, create)
router.patch('/:id', auth, checkUserExist, update)
router.delete('/', auth, checkUserExist, del)
router.post('/login', checkUserExist, login)
module.exports = router